//
//  MyCodeViewController.m
//  HTHappySale
//
//  Created by 曾华桥 on 17/9/26.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "MyCodeViewController.h"

#import <Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

#import "HTTPPromise+Me.h"
#import "Constants.h"
#import "AppUtil.h"
@interface MyCodeViewController ()

@property(nonatomic,strong)UIImageView *photoIv;
@property(nonatomic,strong)UILabel *nickLab;
@property(nonatomic,strong)UILabel *phoneLab;
@property(nonatomic,strong)UIImageView *codeIv;
@end

@implementation MyCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubviews];
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addNavigationMiddleView:@"我的二维码"];
    [self addNavigationLeftView:@"" andType:0];
}

-(void)addSubviews{
    
    //背景遮罩层
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = HTCFQuickRGBColorAlpha(0, 0, 0, 0.6);
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make){
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT);
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
    }];
    
    //内容层
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 10;
    [self.view addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(self.view.mas_left).offset(30);
        make.right.equalTo(self.view.mas_right).offset(-30);
        make.top.equalTo(self.view.mas_top).offset(80);
        make.height.mas_equalTo(SCREEN_WIDTH);
    }];
    
    //头像
    _photoIv = [[UIImageView alloc] init];
    _photoIv.layer.masksToBounds = YES;
    _photoIv.layer.cornerRadius = 10;
    [contentView addSubview:_photoIv];
    [_photoIv mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(contentView.mas_left).offset(32);
        make.top.equalTo(contentView.mas_top).offset(32);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    //昵称
    _nickLab = [[UILabel alloc] init];
    _nickLab.textColor = HTCFQuickRGBColor(33, 33, 33);
    _nickLab.text = @"昵称";
    _nickLab.font = [UIFont systemFontOfSize:16];
    [contentView addSubview:_nickLab];
    [_nickLab mas_makeConstraints:^(MASConstraintMaker *make){
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.top.equalTo(contentView.mas_top).offset(35);
        make.left.equalTo(_photoIv.mas_right).offset(10);
    }];
    
    //电话
    _phoneLab = [[UILabel alloc] init];
    _phoneLab.textColor = HTCFQuickRGBColor(99, 99, 99);
    _phoneLab.text = @"123456789";
    _phoneLab.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:_phoneLab];
    [_phoneLab mas_makeConstraints:^(MASConstraintMaker *make){
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.top.equalTo(_nickLab.mas_bottom).offset(5);
        make.left.equalTo(_photoIv.mas_right).offset(5);
    }];
    
    //二维码
    _codeIv = [[UIImageView alloc] init];
    [contentView addSubview:_codeIv];
    [_codeIv mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(contentView.mas_left).offset(32);
        make.right.equalTo(contentView.mas_right).offset(-32);
        make.top.equalTo(_photoIv.mas_bottom).offset(20);
        make.bottom.equalTo(contentView.mas_bottom).offset(-30);
    }];
}

-(void)loadData{
    __weak __typeof(self)weakSelf = self;
    [HTTPPromise requestMyCodeParams:nil Success:^(id responseObj){
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSLog(@"%@",responseObj);
        int code = [[responseObj objectForKey:@"ret"] intValue];
        if(code  == 0){
            NSString *photoUrl = [[responseObj objectForKey:@"info"] objectForKey:@"portrait"];
            [strongSelf.photoIv sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Img_URL,photoUrl]]];
            strongSelf.nickLab.text = [[responseObj objectForKey:@"info"] objectForKey:@"nickname"];
            strongSelf.phoneLab.text = [[responseObj objectForKey:@"info"] objectForKey:@"mobile"];
            NSArray *imageArray = [[responseObj objectForKey:@"image"] componentsSeparatedByString:@","];
            NSData *imageData = [[NSData alloc] initWithBase64EncodedString:imageArray[1] options:NSDataBase64DecodingIgnoreUnknownCharacters];
            strongSelf.codeIv.image = [UIImage imageWithData:imageData];
        }else{
            [AppUtil showTipMessage:responseObj[@"desc"]];
        }
    } failure:^(NSError *error){
        NSLog(@"%@",error.userInfo);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
